package com.coder520.attend.service;

import com.coder520.attend.mapper.AttendMapper;
import com.coder520.attend.pojo.Attend;
import com.coder520.attend.vo.QueryCondtion;
import com.coder520.common.page.PageQueryBean;
import com.coder520.common.untils.DateUtils;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/5/19.
 */
@Service("attendServiceImpl")

public class AttendServiceImpl implements AttendService {

    //设置12:00为上下午打卡节点
    private static final int NOON_HOUR=12;
    private static final int NOON_MINUTE=00;
    //设置状态变量
    private static final int UNNORMAL_STATE =2;

    //设置缺勤时长
    private static final int ABSENCE_TIME=480;
    private static final int ABSENCE_DAY=480;

    private Log log = LogFactory.getLog(AttendServiceImpl.class);
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Autowired
    private AttendMapper attendMapper;

    //插入打卡数据
    @Override

    public void signAttend(Attend attend) throws Exception {


        try {
            Date today = new Date();
            attend.setAttendDate(today);
            attend.setAttendWeak((byte)DateUtils.getweek());

            Attend TodayRecode = attendMapper.selectTodaySignRecode(attend.getUserId());//查询当天，这人有没有打卡记录
            Date noon = DateUtils.getdate(NOON_HOUR,NOON_MINUTE);

            if(TodayRecode == null){
                //如果没有打卡记录
                if(today.compareTo(noon)<=0){
                    //上午打卡
                    attend.setAttendMoring(today);

                }else {
                    //下午打卡
                    attend.setAttendEvening(today);
                }
                 attendMapper.insertSelective(attend);

            }else {//有打卡记录
                if(today.compareTo(noon)<=0){
                    //上午打卡
                    //不跟新到数据库，只记录最早一次打卡时间
                    return;
                }else {
                    //下午打卡
                    TodayRecode.setAttendEvening(today);
                    //跟新到数据库，记录下午最后一次打卡时间
                  attendMapper.updateByPrimaryKeySelective(TodayRecode);

                }

            }

        } catch (Exception e) {
            log.error("用户签到异常",e);
            throw e;
        }

    }

    //查找分页数据
    @Override
    public PageQueryBean findPageListByCondtion(QueryCondtion condtion) {

        int count = attendMapper.selectCountByCondtion(condtion);//先查询数据库中是否有这个数据
        PageQueryBean pageQueryBean = new PageQueryBean();//用于封装查询到的数据
        if(count>0){

            pageQueryBean.setPageSize(condtion.getPageSize());
            pageQueryBean.setCurrentPage(condtion.getCurrentPage());
            pageQueryBean.setTotalRows(count);
            pageQueryBean.setStartRow(condtion.getStartRow());
            List<Attend> attendList = attendMapper.selectListByCondtion(condtion);
            pageQueryBean.setItems(attendList);

            return pageQueryBean;
        }else {
            return null;
        }

    }

    @Override
    @Transactional
    public void checkAttend() {

        //查询缺勤用户ID 插入打卡记录  并且设置为异常 缺勤480分钟
        List<Long> userIdList =attendMapper.selectTodayAbsence();
        if(org.apache.commons.collections.CollectionUtils.isNotEmpty(userIdList)){
            List<Attend> attendList = new ArrayList<>();
            for(Long userId:userIdList){
                Attend attend = new Attend();
                attend.setUserId(userId);
                attend.setAttendDate(new Date());
                attend.setAttendWeak((byte)DateUtils.getweek());
                attend.setAbsence(ABSENCE_DAY);
                attend.setAttendState((byte) UNNORMAL_STATE);
                attendList.add(attend);
            }
            attendMapper.batchInsert(attendList);
        }
        // 检查晚打卡 将下班未打卡记录设置为异常
        List<Attend> absenceList = attendMapper.selectTodayEveningAbsence();
        if(org.apache.commons.collections.CollectionUtils.isNotEmpty(absenceList)){
            for(Attend attend : absenceList){
                attend.setAbsence(ABSENCE_DAY);
                attend.setAttendState((byte) UNNORMAL_STATE);
                attendMapper.updateByPrimaryKeySelective(attend);
            }
        }

    }


}
